package org.xx.armory.spring5.mvc;

import java.util.Objects;

/**
 * 保留原始内容的字符串。
 */
public final class PreservedString
        implements CharSequence {
    private final String value;

    public PreservedString(
            String value
    ) {
        this.value = value != null ? value : "";
    }

    @Override
    public final int length() {
        return this.value.length();
    }

    @Override
    public final char charAt(
            int index
    ) {
        return this.value.charAt(index);
    }

    @Override
    public final CharSequence subSequence(
            int start,
            int end
    ) {
        return this.value.subSequence(start, end);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.value);
    }

    @Override
    public boolean equals(
            Object obj
    ) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof PreservedString)) {
            return false;
        }
        final var other = (PreservedString) obj;
        return Objects.equals(this.value, other.value);
    }

    @Override
    public final String toString() {
        return this.value;
    }
}
